import 'package:flutter/material.dart';

enum CustomIconType { image, text }

enum CustomIconSize { small, middle, large }

class CustomIcon extends StatelessWidget {
  final CustomIconType type;

  final CustomIconSize size;

  final Color? color;

  final Color? backgroundColor;

  final Image? image;

  final Text? text;

  final BorderRadius? borderRadius;

  final EdgeInsets? padding;

  static String _subText(String text, CustomIconSize size) {
    if (size == CustomIconSize.small) return text.substring(0, 1);
    if (text.length > 4) {
      return text.substring(0, 4);
    }
    return text;
  }

  static double _textFontSize(String text, CustomIconSize size) {
    switch (size) {
      case CustomIconSize.small:
        return 12;
      case CustomIconSize.middle:
        return text.length > 2 ? 11 : 14;
      case CustomIconSize.large:
        return text.length > 2 ? 15 : 20;
    }
  }

  EdgeInsets get _padding {
    switch (size) {
      case CustomIconSize.small:
        return type == CustomIconType.image
            ? const EdgeInsets.all(4)
            : const EdgeInsets.all(1);
      case CustomIconSize.middle:
        return type == CustomIconType.image
            ? const EdgeInsets.all(6)
            : const EdgeInsets.all(4);
      case CustomIconSize.large:
        return type == CustomIconType.image
            ? const EdgeInsets.all(12)
            : const EdgeInsets.all(8);
    }
  }

  double get _width => switch (size) {
        CustomIconSize.small => 20,
        CustomIconSize.middle => 40,
        CustomIconSize.large => 60,
      };

  double get _height => switch (size) {
        CustomIconSize.small => 20,
        CustomIconSize.middle => 40,
        CustomIconSize.large => 60,
      };

  const CustomIcon(
      {super.key,
      this.type = CustomIconType.image,
      this.size = CustomIconSize.small,
      this.color,
      this.backgroundColor,
      this.image,
      this.text,
      this.borderRadius,
      this.padding});

  factory CustomIcon.asset(name,
          {key,
          size = CustomIconSize.small,
          Color? color,
          Color? backgroundColor,
          BorderRadius? borderRadius,
          EdgeInsets? padding}) =>
      CustomIcon(
          key: key,
          type: CustomIconType.image,
          size: size,
          color: color,
          backgroundColor: backgroundColor,
          image: Image.asset(
            name,
            fit: BoxFit.contain,
          ),
          borderRadius: borderRadius,
          padding: padding);

  factory CustomIcon.text(text,
          {key,
          size = CustomIconSize.small,
          Color? color,
          Color? backgroundColor,
          BorderRadius? borderRadius,
          EdgeInsets? padding}) =>
      CustomIcon(
          key: key,
          type: CustomIconType.text,
          size: size,
          color: color,
          backgroundColor: backgroundColor,
          text: Text(
            _subText(text, size),
            textAlign: TextAlign.center,
            style: TextStyle(
                color: color ?? Colors.white,
                fontSize: _textFontSize(text, size),
                fontWeight: FontWeight.bold),
          ),
          borderRadius: borderRadius,
          padding: padding);

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return Container(
      decoration: BoxDecoration(
          color: backgroundColor ?? themeData.primaryColor,
          borderRadius: borderRadius ?? BorderRadius.circular(_width / 2)),
      width: _width,
      height: _height,
      padding: padding ?? _padding,
      child: switch (type) {
        CustomIconType.text => Center(child: text),
        CustomIconType.image => ColorFiltered(
            colorFilter: ColorFilter.mode(
              color ?? Colors.white, // 将此处的颜色更改为你想要的颜色
              BlendMode.srcIn,
            ),
            child: image, // 确保你已经在 pubspec.yaml 文件中正确配置了图片路径
          ),
      },
    );
  }
}
